<?php
namespace Api\Service;

/**
 * 商品分类
 */
class CategoryService
{
    /**
     * 商城首页分类
     * @return [type] [description]
     */
    public function face()
    {
        $category = S('category_index');
        if (!$category || true) {
            $category = M('category')
                ->field('id,name,image,thumb,description')
                ->where(['is_delete' => 0])
                ->order('sort desc, id desc')
                ->select();
            S('category_index', $category, 3600);
        }
        return $category ? $category : [];
    }
    /**
     * 获取所有商品分类，树形
     * @return [type] [description]
     */
    public function getAllTree()
    {
        $tree = D('Category')->getAllTree();
        return $tree ? $tree : [];
    }

    /**
     * 获取所有父分类
     * @return [type] [description]
     */
    public function getParents()
    {
        $parents = D('Category')->getParents();
        return $parents ? $parents : [];
    }

    /**
     * 获取子分类
     * @param  [type] $parent_id [description]
     * @return [type]            [description]
     */
    public function getChilds($parent_id)
    {
        $childs = D('Category')->getChilds($parent_id);
        return $childs ? $childs : [];
    }
}
